<template>
  <div>

    <el-card class="box-card">
      <div class="header">
        <el-row type="flex" class="row-header" justify="space-between">
          <el-button type="primary" @click="dialogVisible=true">添加经纪</el-button>
          <div>
            <el-input v-model="agents.name_contains" placeholder="请输入内容" style="width:400px" />
            <el-button style="backgroundColor:#f7f6fa" icon="el-icon-search" @click="searchAgents" />
          </div>
        </el-row></div>
      <div class="table">  <el-table
        :data="agentsList"
        border
        style="width: 100%"
        align="center"
      >

        <el-table-column
          label="图片"
          width="180"
        >
          <template #default="{row}">
            <img :src="row.image" alt="">
          </template></el-table-column>
        <el-table-column
          label="名称"
          width="180"
          prop="name"
        />
        <el-table-column
          label="邮箱"
          prop="email"
        />
        <el-table-column
          label="电话"
          prop="phone"
        />
        <el-table-column
          label="地址"
          prop="address"
        />
        <el-table-column
          label="介绍"
          prop="desc"
        />
        <el-table-column
          label="操作"
        >
          <template #default="{row}">
            <el-button type="primary" @click="editAgents(row.id)">编辑</el-button>
            <el-button type="danger" @click="delAgents(row.id)">删除</el-button>
          </template>

        </el-table-column>

      </el-table></div>

      <el-dialog
        :title="`${formData.id?'编辑经纪':'新增经纪'}`"
        :visible="dialogVisible"
        width="30%"
        :destroy-on-close="delInfo"
        @close="btnCancel"
      >
        <el-form ref="form" :model="formData" label-width="80px" :rules="rules">
          <el-form-item label="名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="formData.email" />
          </el-form-item>
          <el-form-item label="电话">
            <el-input v-model="formData.phone" />
          </el-form-item>
          <el-form-item label="地址">
            <el-input v-model="formData.address" />
          </el-form-item>
          <el-form-item label="描述" prop="desc">
            <el-input v-model="formData.desc" type="textarea" size="medium " />
          </el-form-item>
          <el-form-item label="封面">
            <ImageUpload ref="agentsPhoto" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="btnOK">确 定</el-button>
          <el-button @click="btnCancel">取 消</el-button>
        </span>
      </el-dialog>
      <el-pagination
        layout="prev, pager, next"
        style="text-align:center;"
        :page-size="3"
        :page-count="pagecount"
        :current-page.sync="curpage"
        @current-change="thispage"
      />

    </el-card></div>
</template>

<script>
import ImageUpload from '@/components/ImageUpload'
import { addAgents, getAgentsCount, getAgentsDetail, getAgentsList, delAgents, editAgents } from '@/api/agents'
export default {
  components: { ImageUpload },
  data() {
    return {

      agentsList: [],
      agents: {
        _limit: '3', _start: '0', name_contains: ''
      },
      dialogVisible: false,
      formData: { name: '', email: '', phone: '', address: '', desc: '', image: '' },
      rules: { // 表单验证规则
        name: [
          { required: true, message: '请输入经纪名称', trigger: 'blur' }
        ],
        desc: [{ required: true, message: '请输入经纪描述', trigger: 'blur' }]

      },
      delInfo: false,
      total: 0,
      pagesize: 3,
      pagenum: 1,
      curpage: 2

    }
  },
  computed: {
    pagecount() {
      return Math.ceil(this.total / this.pagesize)
    }
  },

  async  created() {
    this.getAgentsList()
    this.getCount()
  },

  methods: {
    thispage(pagenum) {
      this.pagenum = pagenum
      this.agents._start = String((pagenum - 1) * 3)
      this.getAgentsList()
      console.log(pagenum)
    },
    async  getCount() {
      const name = { name_contains: this.agents.name_contains }
      this.total = Number(await getAgentsCount(name))
    },
    async delAgents(id) {
      try {
        await this.$confirm('确认删除该经纪吗？')
        await delAgents(id)
        this.$message.success('删除成功')
        this.getAgentsList()
        this.getCount()
      } catch (err) {
        console.log(err)
      }
    },

    async  editAgents(id) {
      this.formData = await getAgentsDetail(id)
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.agentsPhoto.fileList = [{ url: this.formData.image }]
      })
    },
    async getAgentsList() {
      this.agentsList = await getAgentsList(this.agents)
    },
    // 搜索事件
    searchAgents() {
      this.agents._start = 0
      this.curpage = 1
      this.getAgentsList()
      this.getCount()
    },

    async btnOK() {
      const fileList = this.$refs.agentsPhoto.fileList
      await this.$refs.form.validate()

      this.formData.image = fileList.length === 0 ? '' : fileList[0].url
      console.log(this.formData.image)
      if (!this.formData.image) {
        this.$message.error('图片为空')
        return
      }

      if (!this.formData.id) {
        await addAgents(this.formData)
        this.getCount()
        this.$message.success('新增成功')
      } else {
        await editAgents(this.formData)
        this.$message.success('编辑成功')
      }
      this.getAgentsList()
      this.delInfo = true
      this.dialogVisible = false
    },
    btnCancel() {
      this.formData = { name: '', email: '', phone: '', address: '', desc: '', image: '' }
      this.$refs.form.resetFields()
      this.dialogVisible = false
      this.delInfo = true
    }
  }

}
</script>

<style lang="scss" scoped>
.box-card{
    margin: 12px;
}
.row-header{
    margin-bottom: 30px;

}
.header{
    border-bottom: 1px solid #ccc;
    margin-bottom: 25px;

}
img{
    width: 100%;
}
.table{
padding-bottom: 23px;
border-bottom: 1px solid #ccc;
}
</style>

